<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\DB;
use LaravelIdea\Helper\App\Models\_IH_BaseModel_C;
use LaravelIdea\Helper\App\Models\_IH_CourseColumn_C;
use LaravelIdea\Helper\App\Models\_IH_LearnVideo_C;

/**
 * 课程激活码
 */
class NetCourseCdkey extends BaseModel
{
    protected $guarded = [];

    protected $appends = ['activeResource', 'effectiveAt'];

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'userId');
    }

    public function group(): BelongsTo
    {
        return $this->belongsTo(NetCourseCdkeyGroup::class, 'groupId');
    }

    public function learnVideo(): BelongsTo
    {
        return $this->belongsTo(LearnVideo::class, 'dataId');
    }

    public function courseColumn(): BelongsTo
    {
        return $this->belongsTo(CourseColumn::class, 'dataId');
    }

    /**
     * @return BaseModel|BaseModel[]|CourseColumn|CourseColumn[]|LearnVideo|LearnVideo[]|_IH_BaseModel_C|_IH_CourseColumn_C|_IH_LearnVideo_C|null
     */
    public function getActiveResourceAttribute()
    {
        if($this->activeDataId) {
            if ($this->type == 1) {
                return LearnVideo::find($this->activeDataId);
            }elseif ($this->type == 2) {
                return CourseColumn::find($this->activeDataId);
            }
        }
        return null;
    }

    protected static function boot()
    {

        parent::boot();

        // 删除的时候删除用户解锁的资源
        static::deleting(function ($model) {
            if ($model->userId && $model->activeDataId) {
                // 课程
                if ($model->type == 1) {
                    unlockLearnVideo($model->userId, $model->activeDataId);
                }
                // 专栏
                elseif ($model->type == 2) {
                    $courseColumn = CourseColumn::find($model->activeDataId);
                    if ($courseColumn) {
                        unlockCourseColumnResource($model->userId, $courseColumn);
                    }
                }
            }

            // 变更group的数量
            $cdkeyGroup = NetCourseCdkeyGroup::find($model->groupId);
            if ($cdkeyGroup) {
                $cdkeyGroup->count = $cdkeyGroup->count - 1;
                $cdkeyGroup->save();
            }
        });
    }

    /**
     * @return Carbon|null
     */
    public function getEffectiveAtAttribute(): ?Carbon
    {
        // 计算日期
        if (!$this->effective_type) return null;
        $expireAt = null;
        if ($this->effective_type == 1) {
            // 天
            if (empty($this->useAt)) $expireAt = now()->addDays($this->effective_days ?? 1);
            else $expireAt = Carbon::make($this->useAt)->addDays($this->effective_days ?? 1);
        }
        if ($this->effective_type == 2) {
            // 小时
            if (empty($this->useAt)) $expireAt = now()->addHours($this->effective_hours ?? 1);
            else $expireAt = Carbon::make($this->useAt)->addHours($this->effective_hours ?? 1);
        }
        elseif ($this->effective_type == 3) {
            // 日期
            $expireAt = $this->effective_date ? Carbon::make($this->effective_date) : now()->addDays();
        }
        elseif ($this->effective_type == 4) {
            // 长期
            $expireAt = now()->addYears(100);
        }
        return $expireAt;
    }
}
